// pages/personnal/myAddress/myAddress.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0
  },

  /**
   * 确定
   */
  chooseSure:function(){
    let chooseId = this.data.chooseId
    let addressId = this.data.addressList[chooseId].addressId
    app.globalData.addressId = addressId
    wx.navigateBack({ changed: true })
  },

  /**
   * 地址选择
   */
  choose: function (event){
    let index = event.currentTarget.dataset.index
    let id = event.currentTarget.dataset.id
    this.setData({
      chooseId: index,
      addressId: id,
      status:1,
    })
  },

  /**
   * 设置默认
   */
  isDefault:function(event){
    let id = event.currentTarget.dataset.id
    let getTitleDetailUrl = app.globalData.baseUrl + "setDefaultAddress"
    let data = {
      cid: wx.getStorageSync('cid'),
      addressId:id
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      this.onShow()
    })
  },

  /**
   * 编辑
   */
  edit: function (event){
    let index = event.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/personnal/addAddress/addAddress?address=' + JSON.stringify(this.data.addressList[index]),
    })
  },

  /**
   * 删除
   */
  delete: function (event){
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认删除改地址吗？',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          let id = event.currentTarget.dataset.id
          let getTitleDetailUrl = app.globalData.baseUrl + "deleteAddress"
          let data = {
            addressId: id
          }
          util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
            that.onShow()
          })
        }
      }
    })
    
  },

  /**
   * 添加地址
   */
  addAddress:function(){
    wx.navigateTo({
      url: '/pages/personnal/addAddress/addAddress',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.choose != null) {
      this.setData({
        choose: 0
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    let getTitleDetailUrl = app.globalData.baseUrl + "addressList"
    let data = {
      cid: wx.getStorageSync('cid')
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      // console.log(res)
      let addressList = res.data.addressList
      this.setData({
        addressList: addressList
      })
      let addressId = app.globalData.addressId
      if (addressId!=null){
        for (var i = 0; i < addressList.length;i++){
          if (addressId == addressList[i].addressId){
            that.setData({
              chooseId: i,
              addressId: addressId,
              status: 1,
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})